package com.common.base.tools.utils.dialog

import android.os.Bundle
import android.view.*
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.viewbinding.ViewBinding
import com.common.base.R
import com.common.base.tools.utils.event.BindEventBus
import com.common.base.tools.utils.event.EventBusUtils

abstract class BaseDialogFragment<VB : ViewBinding> : DialogFragment() {

    var viewBinding: VB? = null


    protected abstract fun viewBinding(inflater: LayoutInflater, container: ViewGroup?): VB

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        viewBinding = viewBinding(inflater, container)
        return viewBinding!!.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (this.javaClass.isAnnotationPresent(BindEventBus::class.java)) {
            EventBusUtils.register(this)
        }
        isCancelable = false
        setBackgroundTransparent()
        setOnSystemUiVisibilityChangeListener()
    }

    override fun onResume() {
        super.onResume()
        settingFullscreen()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (this.javaClass.isAnnotationPresent(BindEventBus::class.java)) {
            EventBusUtils.unregister(this)
        }
        viewBinding = null
    }

    @Suppress("DEPRECATION")
    open fun settingFullscreen() {
        try {
            val uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            dialog?.window?.decorView?.systemUiVisibility = uiOptions
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /*
     * 设置背景全透明
     */
    protected open fun setBackgroundTransparent() {
        try {
            dialog?.window?.setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            dialog?.window?.decorView?.setBackgroundResource(R.color.colorTrans)
            dialog?.window?.setBackgroundDrawableResource(R.color.colorTrans)
            val attributes = dialog?.window?.attributes
            attributes?.dimAmount = 0.0f
            dialog?.window?.attributes = attributes
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /*
     * 如果页面下有编辑框,需要调用该方法,来防止输入完成后状态栏显示的问题
     */
    @Suppress("DEPRECATION")
    open fun setOnSystemUiVisibilityChangeListener() {
        try {
            dialog?.window?.decorView?.setOnSystemUiVisibilityChangeListener {
                settingFullscreen()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    protected open fun hideSoftInput(currentFocus: View? = activity?.currentFocus) {
        try {
            val imm =
                activity?.getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
            currentFocus?.let {
                imm.hideSoftInputFromWindow(it.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


}